
let { addPrograms, updatePrograms, addOperateLog } = require("../../../api/programs/add");
let { getProjectDetail } = require("../../../api/programs/Detail.js");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    pickerShow: false,
    programType: [  "载体项目", "产业项目", "科技项目", "人才项目", "政策项目"],
    systemInfo: null,

    showDialog: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let timestamp = Date.parse(new Date());
    let date = new Date(timestamp);
    //获取年份
    let Y = date.getFullYear();
    //获取月份
    let M =
      date.getMonth() + 1 < 10
        ? "0" + (date.getMonth() + 1)
        : date.getMonth() + 1;

    let rand = "";
    for (let i = 0; i < 3; i++) {
      let r = Math.floor(Math.random() * 10);
      rand += r;
    }

    let Info = wx.getStorageSync("userInfoMy");
    options.xmID ? this.getProjectDetail(options.xmbh) : this.setData({
      systemInfo: {
        xmfl: "载体项目", //项目类别
        bm: Info.bm, //部门
        tname: Info.jname, //报备人员
        rybh: Info.rybh, //人员编号
        xmbh: `xmbh${Y}${M}${rand}`, //项目编号
        xmmc: "", //项目名称
        xmsq: "", //项目诉求
        //  xmly: "", //项目来源
        bID: Info.id, //报备人id
        bbsj: "", //报备时间，不显示字段，提交表单时自动生成当前时间YYY-MM-DD hh:mm:ss
        tjsj: "",
        xmztsja: '',
        bsa: Info.bsa
      },
    });

  },
  getProjectDetail(xmbh) {
    getProjectDetail(xmbh).then((res) => {
      let Detail = { ...res.data[0] };
      try {
        this.setData({
          systemInfo: Detail,
        });
      } catch (error) {
        console.log(error);
      }
    })
  },
  onConfirm(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      'systemInfo.xmfl': value
    })
    this.onCancel()
  },
  openPick() {
    this.setData({
      pickerShow: true
    })
  },
  onCancel() {
    this.setData({
      pickerShow: false
    })
  },
  inputValue(e) {
    const param = e.currentTarget.dataset.name; //字段名
    const value = e.detail; //值
    const key = `systemInfo.${param}`;
    // const msg=`checkData.${param}`;
    this.setData({
      [key]: value,
      // [msg]:'' //清空错误提示
    });
  },

  onPush() {
    try {
      this.data.systemInfo.xmmc === "" ||
        this.data.systemInfo.xmsq === ""
        ? wx.showToast({
          title: "请填写必填项",
          icon: "error",
          duration: 2000, //持续的时间
        })
        : this.EditPrograms();
    } catch (error) {
      console.log(error);
    }
  },
  EditPrograms() {


    this.data.systemInfo.xmID ? updatePrograms(this.data.systemInfo).then((res) => {

      setTimeout(() => {
        wx.redirectTo({
          url: `/pages/programs/detail/detail?xmbh=${this.data.systemInfo.xmbh}`,
        })
      }, 1000); wx.showToast({
        title: "修改成功，即将返回详情页",
        icon: "none",
      });
    }) : this.Add()
  },
  Add() {
    let time = new Date().getTime();
    //将时间戳格式转换成年月日时分秒
    let date = new Date(time);
    let xmztsja = 24 * 60 * 60 * 1000 * 15
    let JZtime = new Date(time + xmztsja)
    let jz_time = `${JZtime.getFullYear()}-${JZtime.getMonth() + 1 < 10
        ? "0" + (JZtime.getMonth() + 1)
        : JZtime.getMonth() + 1
      }-${JZtime.getDate() < 10 ? "0" + JZtime.getDate() : JZtime.getDate()}`;
    let bbTime = `${date.getFullYear()}-${date.getMonth() + 1 < 10
        ? "0" + (date.getMonth() + 1)
        : date.getMonth() + 1
      }-${date.getDate() < 10 ? "0" + date.getDate() : date.getDate()}`;
    let tjTime = `${date.getHours() < 10 ? "0" + date.getHours() : date.getHours()
      }:${date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()}:${date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds()
      }`;
    this.setData({
      "systemInfo.bbsj": bbTime,
      "systemInfo.tjsj": `${bbTime} ${tjTime}`,
      "systemInfo.xmztsja": `${jz_time} ${tjTime}`,
      "systemInfo.tkjzsj": `${jz_time} ${tjTime}`

    });
    addPrograms(this.data.systemInfo).then((res) => {
      wx.showToast({
        title: "新增成功，请填写补充资料",
        icon: "none",
      });
      setTimeout(() => {
        this.setData({ showDialog: true });
      }, 1000);
    }).then(() => {
      addOperateLog({ type: 1, khbh: '暂无', xmbh: this.data.systemInfo.xmbh, xmmc: this.data.systemInfo.xmmc, bm: this.data.systemInfo.bm, tname: this.data.systemInfo.tname, tjsj: this.data.systemInfo.tjsj })
    });
  },
  onClose() {
    wx.redirectTo({
      url: `/pages/programs/forwardevauate/forwardevauate?id=${this.data.systemInfo.bID}&type=77&name=${this.data.systemInfo.tname}`
    })
    this.setData({ showDialog: false });

  },
  onNext() { 
    wx.redirectTo({
      url: `/pages/programs/detail/detail?xmbh=${this.data.systemInfo.xmbh}`,
    })
  },
  onRest() {
    this.data.systemInfo = { ...{} };
    wx.navigateBack({
      delta: 1, // 回退前 delta(默认为1) 页面
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() { },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { },
});
